from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class LoginTest(object):
    def __init__(self):
        self.start_page()
    def start_page(self):
        dr = webdriver.Chrome()  # 指定上面的环境配置
        dr.implicitly_wait(10)
        dr.get("http://localhost:8080/woniusales/")
        self.dr=dr
    def test_login_success(self):
        """输入正确用户名，密码，验证码 点击登录，登录成功,有注销按钮。"""
        self.dr.find_element_by_id("username").send_keys("admin")
        self.dr.find_element_by_id("password").send_keys("admin123")
        self.dr.find_element_by_id("verifycode").send_keys("0000")
        self.dr.find_element_by_xpath('//button[@onclick="doLogin(\'null\')"]').click()
        zhuxiao=self.dr.find_element_by_link_text("注销")
        if zhuxiao.text=="注销":
            print(self.test_login_success.__doc__," [ 成功 ] ")
        else:
            print(self.test_login_success.__doc__," [ 失败 ] ")
    def test_login_fail(self):
        "输入正确用户名，错误的密码，正确的验证码，点击登录，登录失败，有提示（判断提示长度是否大于0）"
        self.dr.find_element_by_id("username").send_keys("admin")
        self.dr.find_element_by_id("password").send_keys("admin1234")
        self.dr.find_element_by_id("verifycode").send_keys("0000")
        self.dr.find_element_by_xpath('//button[@onclick="doLogin(\'null\')"]').click()

        WebDriverWait(self.dr,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'.bootbox-body')))

        tishi=self.dr.find_element_by_css_selector('.bootbox-body').text
        if tishi=="登录失败，请重新登录.":
            print(self.test_login_fail.__doc__," [ 成功 ] ")
        else:
            print(self.test_login_fail.__doc__, " [ 失败 ] ",f"提示信息是：{tishi}")
if __name__ == '__main__':
    LoginTest().test_login_success()
    LoginTest().test_login_fail()




